/**
 * JCaddie.java 21 juil. 07
 *
 * Copyright (c) 2007 windu.2b <jcaddie@windu2b.org>. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.windu2b.jcaddie;

import java.util.*;

import javax.swing.*;

import org.windu2b.jcaddie.io.*;
import org.windu2b.jcaddie.model.*;
import org.windu2b.jcaddie.swing.*;

/**
 * @author windu.2b
 * 
 */
public class JCaddie extends CaddieApplication
{
	private CaddieRecorder	caddieRecorder;


	private UserPreferences	userPreferences;




	/**
	 * 
	 */
	private JCaddie()
	{
		caddieRecorder = new CaddieFileRecorder();
		userPreferences = new FileUserPreferences();
	}




	/**
	 * @see org.windu2b.jcaddie.model.CaddieApplication#getCaddieRecorder()
	 */
	@Override
	public CaddieRecorder getCaddieRecorder()
	{
		return caddieRecorder;
	}




	/**
	 * @see org.windu2b.jcaddie.model.CaddieApplication#getUserPreferences()
	 */
	@Override
	public UserPreferences getUserPreferences()
	{
		return userPreferences;
	}



	// Only one application may be created with main method or
	// SingleInstanceService
	private static CaddieApplication	application;




	public static void main( String[] args )
	{
		// At first main call
		if ( application == null )
		{
			initLookAndFeel();
			application = createApplication();
		}

		if ( args.length == 2 && args[0].equals( "-open" ) )
		{
			try
			{
				// Read caddie file in args [1] if args [0] == "-open"
				Caddie caddie = application.getCaddieRecorder().readCaddie(
				        args[1] );
				caddie.setName( args[1] );
				application.addCaddie( caddie );
			}
			catch ( RecorderException ex )
			{
				// Show an error message dialog if caddie couldn't be read
				ResourceBundle resource = ResourceBundle
				        .getBundle( CaddieController.class.getName() );
				String message = String.format( resource
				        .getString( "openError" ), args[1] );
				JOptionPane.showMessageDialog( null, message, "JCaddie",
				        JOptionPane.ERROR_MESSAGE );
			}
		}
		else
		{
			// Create a default caddie
			Caddie caddie = new Caddie();
			application.addCaddie( caddie );
		}
	}




	/**
	 * Returns main application object.
	 */
	private static CaddieApplication createApplication()
	{

		final CaddieApplication application = new JCaddie();

		application.addCaddieListener( new CaddieListener()
		{
			public void caddieChanged( CaddieEvent ev )
			{
				switch ( ev.getType() )
				{
					case ADD :
						Caddie caddie = ev.getCaddie();
						new CaddieFrameController( caddie, application );
						break;

					case DELETE :
						if ( application.getCaddies().isEmpty() )
						    System.exit( 0 );
						break;
				}
			};
		} );

		return application;
	}




	/**
	 * Sets application look anf feel and various <code>System</code>
	 * properties.
	 */
	private static void initLookAndFeel()
	{
		// Enables Java 5 bug correction about dragging directly
		// a tree element without selecting it before :
		// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4521075
		System.setProperty( "sun.swing.enableImprovedDragGesture", "true" );
		// Change Mac OS X application menu name
		System.setProperty( "com.apple.mrj.application.apple.menu.about.name",
		        "JCaddie" );
		// Use Mac OS X screen menu bar for frames menu bar
		System.setProperty( "apple.laf.useScreenMenuBar", "true" );
		try
		{
			// Apply current system look and feel
			UIManager
			        .setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
		}
		catch ( Exception e )
		{
			// Too bad keep current look and feel
		}
	}
}